package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtil {

    public static final String MYBATIS_CONFIG_FILE= "mybatis-config.xml";
    private static SqlSessionFactory FACTORY;

    static {
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream(MYBATIS_CONFIG_FILE);
            FACTORY = new SqlSessionFactoryBuilder().build(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getFactory(){
        return FACTORY;
    }

    public static SqlSession getSession(){
        return FACTORY.openSession(true);
    }
}
